/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p1;

import java.util.ArrayList;

/**
 *
 * @author Derek Ward
 * 
 * Represents a solution to a problem
 * 
 */
public class Solution 
{
    private     ArrayList<String>       m_solutionSet;
    private     int                     m_solutionWeight;
    
    public Solution()
    {
        m_solutionSet = new ArrayList<String>();
    }
    
    public Solution(Node source, Node dest)
    {
        m_solutionSet = new ArrayList<String>();
        m_solutionWeight = -1;
        
        findSolution(source, dest);
    }
    
    private void findSolution(Node source, Node dest)
    {
        double          totalDistance;
        Node            tn;
        int             ew;
        
        // do sanity checks!
        
        totalDistance = dest.m_min;
        tn = dest;
        
        while ( tn != null && !tn.getLabel().equals(source.getLabel()) )
        {
            //s = tn.getLabel() + " " + s;
            m_solutionSet.add(0, tn.getLabel());
            ew = tn.getEdgeWeight(tn.m_previousNode);
            //s = ew + " " + s;
            m_solutionSet.add(0, Integer.toString(ew));
            tn = tn.m_previousNode;
        }
        
        m_solutionSet.add(0, source.getLabel());
        
        if ( totalDistance == Double.POSITIVE_INFINITY )
        {
            m_solutionSet.clear();
            m_solutionSet.add("[" + dest.getLabel() + "]");
            m_solutionWeight = -1;
            return;
        }
        
        m_solutionWeight = (int)totalDistance;
    }
    
    public ArrayList<String> getNodeLabelList()
    {
        ArrayList<String>       nodeLabelList = new ArrayList<String>();
        
        for (String s : m_solutionSet)
        {
            if ( isParsableToInt(s) )
                continue;
            else
                nodeLabelList.add(s);
        }
        
        return nodeLabelList;
    }
    
    public int getSolutionWeight()
    {
        return m_solutionWeight;
    }
    
    public String toStringSansTotalSansFirstSansLast()
    {
        String ts = "";
        
        for ( int i = 1; i < (m_solutionSet.size() - 1); i++ )
            ts = ts + m_solutionSet.get(i) + " ";
        
        if ( m_solutionWeight == -1 )
            ts = " " + m_solutionSet.get(0) + " ";
        
        return ts;
    }
    
    public String toStringSansTotalSansLast()
    {
        String ts = "";
        
        for ( int i = 0; i < (m_solutionSet.size() - 1); i++ )
            ts = ts + m_solutionSet.get(i) + " ";
        
        if ( m_solutionWeight == -1 )
            ts = " " + m_solutionSet.get(0) + " ";
        
        return ts;
    }
    
    public String toStringSansTotalSansFirst()
    {
        String ts = "";
        
        for ( int i = 1; i < m_solutionSet.size(); i++ )
            ts = ts + m_solutionSet.get(i) + " ";
        
        if ( m_solutionWeight == -1 )
            ts = " " + m_solutionSet.get(0) + " ";
        
        return ts;
    }
    
    public String toStringSansTotal()
    {
        String ts = "";
        
        for (String s : m_solutionSet)
            ts = ts + s + " ";
        
        if ( m_solutionWeight == -1 )
            ts = " " + m_solutionSet.get(0) + " ";
        
        return ts;
    }
    
    @Override
    public String toString()
    {
        String ts = Integer.toString(m_solutionWeight) + " ";
        
        if ( m_solutionWeight == -1 )
            ts = "";
        
        for (String s : m_solutionSet)
            ts = ts + s + " ";
        
        if ( m_solutionWeight == -1 )
            ts = " " + m_solutionSet.get(0) + " ";
        
        return ts;
    }
    
    public boolean isParsableToInt(String i)
    {
        try
        {
            Integer.parseInt(i);
        }
        catch(NumberFormatException nfe)
        {
            return false;
        }
        return true;
    }
    
    public boolean isSolutionGood()
    {
        if ( m_solutionWeight == -1 )
            return false;
        
        return true;
    }
    
    @Override
    public Solution clone()
    {
        Solution sl = new Solution();
        
        for ( String s : m_solutionSet )
            sl.m_solutionSet.add(s);
        
        sl.m_solutionWeight = m_solutionWeight;
        
        return sl;
    }
}
